package cc.ufinity.networkdiagnose.adapter;

import java.util.List;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.view.R;

public class APListAdapter extends BaseAdapter {

	private Context mContext;
	private List<ScanResult> mList;
	
	// ���ز���
	LayoutInflater vi;
	
	public APListAdapter(Context context,List<ScanResult> list)
	{
		mContext = context;
		mList = list;
	}
	
	@Override
	public int getCount() {
		
		if(mList == null)
			return 0;
		return mList.size();
	}
	
	@Override
	public Object getItem(int position) {
		if(mList == null)
			return null;
		else
			return mList.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view;
		
		if(convertView == null)
		{
			vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = (LinearLayout) vi.inflate(R.layout.ap_list_item, null);
		} else {
			view = convertView;
		}
		
		FillDataIntoView(view,position);
		
		return view;
	}
	
	public void FillDataIntoView(View view, int position) {
		final ScanResult ap = mList.get(position);
		
		TextView tv_ssid = (TextView)view.findViewById(R.id.ap_ssid);
		TextView tv_mac = (TextView)view.findViewById(R.id.ap_mac);
		TextView tv_level = (TextView)view.findViewById(R.id.ap_level);
		TextView tv_channle = (TextView)view.findViewById(R.id.ap_channle);

		tv_ssid.setText(ap.SSID);
		tv_mac.setText(ap.BSSID);
		tv_level.setText(ap.level +" dBm");
		tv_channle.setText(getFrequency(ap.frequency) +"");
	}
	
	//信道的转换
	private int getFrequency(int a){
		int channel=1;
		 if(a==2412){
			 channel=1;
		}else if(a==2417){
			 channel=2;
		}else if(a==2422){
			 channel=3;
		}else if(a==2427){
			 channel=4;
		}else if(a==2432){
			 channel=5;
		}else if(a==2437){
			 channel=6;
		}else if(a==2442){
			 channel=7;
		}else if(a==2447){
			 channel=8;
		}else if(a==2452){
			 channel=9;
		}else if(a==2457){
			 channel=10;
		}else if(a==2462){
			 channel=11;
		}else if(a==2467){
			 channel=12;
		}else if(a==2472){
			 channel=13;
		}else if(a==2484){
			 channel=14;
		}
	 return channel;
	}

}
